package com.gaattc.controller.mall;

import com.gaattc.model.Result;
import com.gaattc.model.bo.AskGoodsMsgBO;
import com.gaattc.model.bo.SendCommentBO;
import com.gaattc.model.vo.*;
import com.gaattc.service.GoodsService;
import com.gaattc.service.GoodsServiceImpl;
import com.gaattc.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;

@WebServlet("/api/mall/goods/*")
public class GoodsServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsServiceImpl();

    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/goods/", "");
        if("askGoodsMsg".equals(action)) {
            askGoodsMsg(request, response);
        }
    }

    private void askGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String body = HttpUtils.getRequestBody(request);
        AskGoodsMsgBO askGoodsMsgBO = gson.fromJson(body, AskGoodsMsgBO.class);
        goodsService.askGoodsMsg(askGoodsMsgBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/goods/", "");
        if ("getGoodsByType".equals(action)) {
            getGoodsByType(request, response);
        } else if ("getGoodsInfo".equals(action)) {
            getGoodsInfo(request, response);
        } else if ("getGoodsMsg".equals(action)) {
            getGoodsMsg(request, response);
        } else if("getGoodsComment".equals(action)) {
            getGoodsComment(request, response);
        } else if("searchGoods".equals(action)) {
            searchGoods(request, response);
        }
    }

    /**
     * 查询商品
     * @param request req
     * @param response resp
     */
    private void searchGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String keyword = request.getParameter("keyword");
        List<GetGoodsByTypeVO> result = goodsService.searchGoods(keyword);
        response.getWriter().println(gson.toJson(Result.ok(result)));
    }

    /**
     * 获取评价
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void getGoodsComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int goodsId = Integer.parseInt(request.getParameter("goodsId"));
        GetGoodsCommentVO commentVO = goodsService.getGoodsComment(goodsId);
        response.getWriter().println(gson.toJson(Result.ok(commentVO)));
    }

    /**
     * 获取商品问答
     * @param request  req
     * @param response resp
     * @throws IOException io
     */
    private void getGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        List<GetGoodsMsgVO> getGoodsMsgVO = goodsService.getGoodsMsg(id);
        response.getWriter().println(gson.toJson(Result.ok(getGoodsMsgVO)));
    }

    /**
     * 获取商品信息
     * @param request  req
     * @param response resp
     * @throws IOException io
     */
    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        UserGetGoodsInfoVO goodsInfo = goodsService.userGetGoodsInfo(id);
        response.getWriter().println(gson.toJson(Result.ok(goodsInfo)));
    }

    /**
     * 获取类型下商品信息
     *
     * @param request  req
     * @param response resp
     * @throws IOException io
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            int typeId = Integer.parseInt(request.getParameter("typeId"));
            List<GetGoodsByTypeVO> list = goodsService.getGoodsByType(typeId);
            response.getWriter().println(gson.toJson(Result.ok(list)));
        } catch (Exception e) {
            response.getWriter().println(gson.toJson(Result.error("参数不合法")));
        }
    }
}
